<template>
  <div>

    <el-dialog
      :title="title"
      :visible.sync="open"
      width="800px"
      append-to-body
      :before-close="close"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="150px"
        class="demo-ruleForm"
      >
        <el-form-item label="商品和包装" prop="entrust_company">
          <div style="display: inline-block;width: 50%;" @click="selGoods()">
            <el-input v-model="form.goods_name" type="text"  readonly />
          </div>
<!--          <el-select-->
<!--            v-model="form.goods_name"-->
<!--            filterable-->
<!--            placeholder="商品"-->
<!--            clearable-->
<!--            style="width: 50%"-->
<!--            @change="getPacking(1)"-->
<!--          >-->
<!--            <el-option-->
<!--              v-for="(item, index) in selectPro"-->
<!--              :key="index"-->
<!--              :label="item.label"-->
<!--              :value="item.value"-->
<!--            />-->
<!--          </el-select>-->
          <el-select
            v-model="form.packing_id"
            filterable
            placeholder="包装"
            clearable
            style="width: 50%"
            @change="ck_info"
          >
            <el-option
              v-for="(item, index) in selectPick"
              :key="index"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item v-if="showSku" label="SKU">
          <span>{{ sku }}</span>
        </el-form-item>
        <el-form-item v-if="Qty !== false">
          <span slot="label"> 库存数量 </span>
          <span>{{ QList.Good_Qty + QList.Hold_Qty + QList.NG_Qty }}(Good:
            {{ QList.Good_Qty }} Hold: {{ QList.Hold_Qty }} NG:
            {{ QList.NG_Qty }})</span>
        </el-form-item>
        <el-form-item prop="number">
          <span slot="label"> 出库商品数量</span>
          <!-- oninput="value=this.value.replace(/\D+/g,'');if(value.length>11)value=value.slice(0,11)" -->
          <el-input
            v-model="form.number"
            type="number"
            @input="textNum($event)"
          />
        </el-form-item>
        <el-form-item prop="box_number">
          <span slot="label"> 包装数量 </span>
          {{ stary }} 托 {{ box }} 箱
        </el-form-item>
        <el-form-item>
          <span slot="label"> 剩余数量 </span>
          {{ surplus }}
        </el-form-item>

        <el-row v-if="conditions">
          <el-form-item label="入库单号">
            <el-cascader
              v-model="form.warehousing_select_no"
              placeholder="请选择入库单号"
              :options="warehousing_no_list"
              clearable
              filterable
              collapse-tags
              :props="{
                expandTrigger: 'hover',
                checkStrictly: true,
                multiple: true,
              }"
            />
          </el-form-item>
        </el-row>

        <el-row type="flex" justify="end">
          <el-row>
            <el-col>
              <el-button
                size="medium"
                type="text"
                @click="conditions = !conditions"
              >出库筛选</el-button>
            </el-col>
          </el-row>
        </el-row>

        <el-row style="top: 30px; margin-bottom: 30px; text-align: center">
          <el-col :span="24">
            <el-button
              type="primary"
              size="medium"
              @click="submitForm('form')"
            >编辑</el-button>
            <el-button size="medium" @click="close()">取消</el-button>
          </el-col>
        </el-row>
      </el-form>

    </el-dialog>
    <div v-if="open1">
      <!-- <template slot-scope="scope"> -->
      <div v-if="open1" style="display: flex;">

        <el-button
          v-if="btnOpen"
          v-action="'pick@index'"
          size="mini"
          type="danger"
          @click="editBtnDisabled = !editBtnDisabled"
        >编辑
        </el-button>
        <el-button
          v-if="btnOpen"
          v-action="'pick@index'"
          :disabled="editBtnDisabled"
          size="mini"
          type="primary"
          @click="submitForm('form')"
        >保存
        </el-button>
        <el-button
          v-if="btnOpen"
          v-action="'pick@index'"
          size="mini"
          type="success"
          @click="picking()"
        >拣货
        </el-button>
        <el-button
          size="mini"
          type="info"
          @click="seeInfo()"
        >捡货列表
        </el-button>
        <el-button
          v-if="form.if_back_sn == 1"
          size="mini"
          type="warning"
          @click="pickcode()"
        >捡货编号
        </el-button>
        <el-row type="flex" justify="end">
          <el-row>
            <el-col>
              <el-button
                size="medium"
                type="text"
                style="background-color: #FFF8E6; color: #FFD666;"
                @click="conditions = !conditions"
              >出库筛选</el-button>
            </el-col>
          </el-row>
        </el-row>
      </div>

      <el-form
        v-if="open1"
        ref="form"
        size="mini"
        :disabled="editBtnDisabled"
        :model="form"
        :rules="rules"
        label-width="100px"
        class="demo-ruleForm2"
      >
        <el-row>

          <el-col :span="12" style="border-right: 1px solid #efe9e9;padding: 0 50px;">

            <el-row>
              <el-col :span="12">
                <el-form-item label="商品名称" prop="entrust_company">
                  <div class="select-style">
                    <el-select
                      v-model="form.goods_name"
                      filterable
                      placeholder="商品名称"
                      clearable
                      style="display: inline-block;"
                      @change="getPacking(1)"
                    >
                      <el-option
                        v-for="(item, index) in selectPro"
                        :key="index"
                        :label="item.label"
                        :value="item.value"
                      />
                    </el-select>

                  </div>
                </el-form-item>
                <el-form-item>
                  <span slot="label">
                    SKU
                  </span>
                  <el-input v-model="form.sku" disabled />
                </el-form-item>
                <el-col :span="12" />
                <el-form-item>
                  <span slot="label">
                    商品描述
                  </span>
                  <el-input v-model="form.describe" disabled />
                </el-form-item>
                <el-form-item>
                  <span slot="label">
                    商品中文名称
                  </span>
                  <el-input v-model="form.goods_Cname" disabled />
                </el-form-item>
                <el-form-item>
                  <span slot="label">
                    商品英文名称
                  </span>
                  <el-input v-model="form.goods_Ename" disabled />
                </el-form-item>
                <el-form-item>
                  <span slot="label">
                    HSCODE
                  </span>
                  <el-input v-model="form.HSCODE" disabled />
                </el-form-item>
<!--                <el-form-item>-->
<!--                  <span slot="label">-->
<!--                    项号-->
<!--                  </span>-->
<!--                  <el-input v-model="form.new_item_number" disabled />-->
<!--                </el-form-item>-->

                <el-form-item>
                  <span slot="label">
                    成品料号
                  </span>
                  <el-input v-model="form.PO"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item>
                  <span slot="label">
                    料号体系
                  </span>
                  <el-input v-model="form.item_number_system" disabled />
                </el-form-item>
                <el-form-item>
                  <span slot="label">
                    供应商
                  </span>
                  <el-input v-model="form._supplier_name" disabled />
                </el-form-item>
                <el-form-item>
                  <span slot="label">
                    制造商
                  </span>
                  <el-input v-model="form._manufacturer_name" disabled />
                </el-form-item>
              </el-col>
            </el-row>

          </el-col>

          <el-col style="padding: 0 50px;" :span="12">

            <el-row>

              <el-col :span="12">

                <el-form-item prop="number">
                  <span slot="label"> 出库商品数量 </span>
                  <!-- oninput="value=this.value.replace(/\D+/g,'');if(value.length>11)value=value.slice(0,11)" -->
                  <el-input
                    v-model="form.number"
                    type="number"
                    @input="textNum($event)"
                  />
                </el-form-item>
                <el-form-item prop="number">
                  <span slot="label"> 包装 </span>
                  <!-- oninput="value=this.value.replace(/\D+/g,'');if(value.length>11)value=value.slice(0,11)" -->
                  <el-select
                    v-model="form.packing_id"
                    filterable
                    placeholder="包装"
                    clearable
                    style="display: inline-block;"
                    @change="ck_info"
                  >
                    <el-option
                      v-for="(item, index) in selectPick"
                      :key="index"
                      :label="item.label"
                      :value="item.value"
                    />
                  </el-select>
                </el-form-item>
                <el-form-item>
                  <span slot="label">
                    库存数量
                  </span>
                  <el-input disabled :placeholder="QList.Good_Qty + QList.Hold_Qty + QList.NG_Qty+'(Good:'+QList.Good_Qty+'Hold:'+QList.Hold_Qty+'NG:'+QList.NG_Qty" />
                </el-form-item>
                <el-form-item>
                  <span slot="label"> 货物余量 </span>
                  <!-- {{ surplus }} -->
                  <el-input v-model="surplus" disabled />
                </el-form-item>
<!--                <el-form-item>-->
<!--                  <span slot="label">-->
<!--                    PO-->
<!--                  </span>-->
<!--                  <el-input v-model="form.PO" />-->
<!--                </el-form-item>-->
                <el-form-item>
                  <span slot="label">
                    DO
                  </span>
                  <el-input v-model="form.DO" />
                </el-form-item>
                <el-form-item>
                  <span slot="label">
                    CO
                  </span>
                  <el-input v-model="form.CO" />
                </el-form-item>
              </el-col>
              <el-col :span="12">

<!--                <el-form-item>-->
<!--                  <span slot="label">-->
<!--                    SN-->
<!--                  </span>-->
<!--                  <el-input v-model="form.SN" />-->
<!--                </el-form-item>-->
                <el-form-item>
                  <span slot="label">
                    LOT1
                  </span>
                  <el-input v-model="form.LOT1" />
                </el-form-item>

                <el-form-item>
                  <span slot="label">
                    LOT2
                  </span>
                  <el-input v-model="form.LOT2" />
                </el-form-item>
                <el-form-item>
                  <span slot="label">
                    LOT3
                  </span>
                  <el-input v-model="form.LOT3" />
                </el-form-item>
              </el-col>

            </el-row>

          </el-col>
        </el-row>
        <!-- v-if="showSku" -->
        <!-- <el-form-item label="SKU">
          <span>{{ sku }}</span>
        </el-form-item> -->

        <!-- <el-form-item v-if="Qty !== false"> -->

        <!-- <el-form-item>
          <span slot="label"> 库存数量 </span>
          <span>{{ QList.Good_Qty + QList.Hold_Qty + QList.NG_Qty }}(Good:
            {{ QList.Good_Qty }} Hold: {{ QList.Hold_Qty }} NG:
            {{ QList.NG_Qty }})</span>
        </el-form-item> -->

        <!-- <el-form-item prop="box_number">
          <span slot="label"> 包装数量 </span>
          {{ stary }} 托 {{ box }} 箱
        </el-form-item> -->

        <el-row v-if="conditions">
          <el-form-item label="入库单号">
            <el-cascader
              v-model="form.warehousing_select_no"
              placeholder="请选择入库单号"
              :options="warehousing_no_list"
              clearable
              filterable
              collapse-tags
              :props="{
                expandTrigger: 'hover',
                checkStrictly: true,
                multiple: true,
              }"
            />
          </el-form-item>
        </el-row>

        <!-- <el-row style="top: 30px;margin-bottom: 30px;text-align:center;">
        <el-col :span="24">
          <el-button type="primary" size="medium" @click="submitForm('form')">{{ title }}</el-button>
          <el-button size="medium" @click="close()">取消</el-button>
        </el-col>
      </el-row> -->

      </el-form>

      <!-- <el-form v-if="open1" label-position="left" inline class="demo-table-expand">
        <el-form-item label="出库主单编号">
          <span>{{ form.warehouse_out_id }}</span>
        </el-form-item>
        <el-form-item label="出库明细编号">
          <span>{{ form.warehouse_out_son_id }}</span>
        </el-form-item>
        <el-form-item label="商品编号">
          <span>{{ form.goods_id }}</span>
        </el-form-item>
        <el-form-item label="HSCODE">
          <span>{{ form.HSCODE }}</span>
        </el-form-item>
        <el-form-item label="商品中文名称">
          <span>{{ form.goods_Cname }}</span>
        </el-form-item>
        <el-form-item label="商品英文名称">
          <span>{{ form.goods_Ename }}</span>
        </el-form-item>
        <el-form-item label="供应商名称">
          <span>{{ form.supplier_name }}</span>
        </el-form-item>
        <el-form-item label="制造商名称">
          <span>{{ form.manufacturer_name }}</span>
        </el-form-item>
        <el-form-item label="包装编号">
          <span>{{ form.packing_id }}</span>
        </el-form-item>
        <el-form-item label="包装条形码">
          <span>{{ form.p_barcode }}</span>
        </el-form-item>
        <el-form-item label="创建时间">
          <span>{{ form.created_at }}</span>
        </el-form-item>
      </el-form> -->

    </div>

    <el-dialog
      title="拣货编号"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose">
      <el-input type="textarea" v-model="back_sn" :rows="4"></el-input>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" size="mini" @click="addCode()">确 定</el-button>
      </span>
    </el-dialog>

    <SeeInfo
      :opens.sync="opens"
      :titles="titles"
      :lists="lists"
      :outsonid="currentPickWarehouseOutSonId"
    />
    <ProductDialog :visible.sync="dialogVisibles" :on-confirm="handleConfirm" :diaclose="diaclose"/>
  </div>
</template>
<script>
import {
  createWarehouseOutSon,
  updateWarehouseOutSon,
  delWarehouseOutSon,
  inventoryOperationRecordList,
  getAllWarehousingNo
} from '@/api/storehouse'
import { getGPSelect, getGoods, getPacking, getInventory } from '@/api/product'
import { getIndexlist } from '@/api/storehouse'
import SeeInfo from './SeeInfo.vue'
import ProductDialog from '@/components/ProductDialog/index.vue'
import Product from "@/views/storehouse/warehousingGoods/product";
export default {
  name: 'AddEdit',
  components: {
    SeeInfo,
    Product,
    ProductDialog
  },
  props: {
    open1: {
      type: Boolean,
      default: true
    },
    open: {
      type: Boolean
    },
    form: {
      type: Object
    },
    title: {
      type: String
    },
    warehouseoutid: {
      type: String
    },
    residue: {
      type: Number,
      default: 0
    },
    existNumber: {
      type: Number,
      default: 0
    }
  },
  data() {
    // const self = this
    return {
      dialogVisible: false,
      dialogVisibles: false,
      lists: [],
      btnOpen: false,
      currentPickWarehouseOutSonId: '',
      _warehouse_out_id: '',
      emptyHide: true,
      forms: {},
      opens: false,
      conditions: false,
      titles: '',
      pickerOptions: {
        shortcuts: [{
          text: '今天',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime())
            end.setTime(end.getTime() + 3600 * 1000 * 24)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '三个月前',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            end.setTime(end.getTime() - 3600 * 1000 * 24 * 90)
            start.setTime(Date.parse('2000/01/01'))
            picker.$emit('pick', [start, end])
          }
        }]
      },
      rules: {
        number: [
          { required: true, message: '请填写商品数量', trigger: 'blur' }
        ],
        box_number: [
          { required: true, message: '请填写商品箱数', trigger: 'blur' }
        ]
      },
      selectData: [],
      selectPro: [],
      selectPick: [],
      num: 0,
      isLoad: true,
      packing_id: '',
      Qty: false,
      QList: [],
      showSku: false,
      sku: '',
      // open1: true,
      warehousing_no_list: [],
      editBtnDisabled: true,
      AllNumber: 0,
      InputNum: 0,
      back_sn: ''
    }
  },
  computed: {
    surplus() {
      console.log(this.title)
      console.log(2, this.residue, this.existNumber, this.form.number)
      if (this.title == '编辑') {
        return this.residue + this.existNumber - parseInt(this.form.number)
      } else {
        if (this.form.number) {
          return this.residue - parseInt(this.form.number)
        } else {
          console.log(this.residue)

          return this.residue
        }
      }
    },
    stary() {
      var num = 0
      if (this.selectPick instanceof Array) {
        this.selectPick.forEach((item) => {
          if (item.value === this.form.packing_id) {
            num = Math.ceil(this.form.number / item.packing_num / item.layup_num)
          }
        })
        if (isNaN(num)) {
          return 0
        } else {
          return num
        }
      } else {
        return 0
      }
    },
    box() {
      var num = 0
      if (this.selectPick instanceof Array) {
        this.selectPick.forEach((item) => {
          if (item.value === this.form.packing_id) {
            num = Math.ceil(this.form.number / item.packing_num)
          }
        })
        if (isNaN(num)) {
          return 0
        } else {
          return num
        }
      } else {
        return 0
      }
    }
  },
  watch: {
    'form.number': {
      deep: true,
      handler(val) {
        this.ck_num()
      }
    },
    title: {
      handler(val) {
        if (val == '编辑') {
          this.getPacking(2)
        }
      }
    }
  },
  updated() {
    // this.selectPick = this.form.packing_list
  },
  mounted() {
    // console.log(this.open1)
    this.selectPick = this.form.packing_list
    // console.log(444, this.selectPick)
    if (this.form.status == 1) {
      this.btnOpen = false
    } else {
      this.btnOpen = true
    }
    if (this.form.goods_id && this.form.packing_id) {
      getInventory({
        warehouse_out_id: this.warehouseoutid,
        goods_id: this.form.goods_id,
        packing_id: this.form.packing_id
      }).then((response) => {
        if (response.code === 10000) {
          this.Qty = response.data.Good_Qty
          this.QList = response.data
        }
      })
    }
  },
  created() {
    //this.getGoods()
    this.getAllWarehousingNo()
    // this.getProductpPcking()
    if (this.$route.query.warehouse_out_id) {
      // 存在，编辑啊
      // this.form = JSON.parse(this.$route.query._form)
      // this.title = '编辑'
    }
  },
  methods: {
    selGoods() {
      this.dialogVisibles = true
    },
    handleConfirm(val) {
      let that = this
      that.form.goods_name = val.goods_name
      that.form.goods_id = val.goods_id
      that.selectPick = []
      //this.form.packing_id = ''
      getPacking({ goods_name: that.form.goods_name,goods_id:val.goods_id }).then(response => {
        if (response.code === 10000) {
          var data = JSON.parse(JSON.stringify(response.data))
          data.forEach((item) => {
            item.label = item.label
            item.value = item.packing_id
          })
          that.selectPick = data
        }
      })
      that.dialogVisibles = false
    },
    diaclose(){
      this.dialogVisibles = false
    },
    getForm(form) {
      this.form = form
      console.log('this.form', this.form)
    },
    getQuery(item) {
      // this.form = JSON.parse(JSON.stringify(item))
      this.selectPick = JSON.parse(JSON.stringify(item)).packing_list
      if (this.form.status == 1) {
        this.btnOpen = false
      } else {
        this.btnOpen = true
      }
      console.log(33, this.btnOpen)
    },
    /** 删除按钮操作 */
    handleDel() {
      const that = this
      that
        .$confirm('是否删除该数据', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        .then(function() {
          that.loading = true
          delWarehouseOutSon({ id: this.form.id.toString() }).then(
            (response) => {
              that.loading = false
              if (response.code === 10000) {
                that.$notify.success(response.message)
                that.getList()
              } else {
                that.$notify.error(response.message)
              }
            }
          )
        })
        .catch(() => {})
    },
    picking() {
      console.log(this.form)
      this.$router.push({
        path: '/stock/pick',
        query: {
          warehouse_out_id: this.form.warehouse_out_id,
          warehouse_out_son_id: this.form.warehouse_out_son_id
          // type:1,
          // number: this.form.number
        }
      })
    },
    seeInfo() {
      getIndexlist({
        warehouse_out_son_id: this.form.warehouse_out_son_id
      }).then((response) => {
        if (response.code === 10000) {
          this.lists = response.data
          this.opens = true
          this.titles = '拣货详情'
          this.currentPickWarehouseOutSonId = this.form.warehouse_out_son_id || ''
        }
      })
    },
    async getAllWarehousingNo() {
      const that = this
      try {
        const { data, code } = await getAllWarehousingNo()
        if (code === 10000) {
          data.forEach((item) => {
            that.warehousing_no_list.push({
              label: item,
              value: item
            })
          })
        }
      } catch (err) {
        that.$notify.error(err.message)
      }
    },
    textNum(event) {
      if (event > this.residue + this.existNumber) {
        this.$notify({
          message: '出库数量不能大于剩余数量',
          type: 'warning'
        })
        this.form.number = this.residue
      }
      var num = this.QList.Good_Qty + this.QList.Hold_Qty + this.QList.NG_Qty
      if (event > num) {
        this.$notify({
          message: '出库数量不能大于库存数量',
          type: 'warning'
        })
        this.form.number = num
      }
    },
    ck_num() {
      if (!this.form.number) {
        return false
      }
      if (!this.form.packing_id) {
        return false
      }
      if (this.selectPick.length == 0) {
        return false
      }
      var packing_num = 0
      var layup_num = 0
      var data = JSON.parse(JSON.stringify(this.selectPick))
      data.forEach((item) => {
        if (item.value == this.form.packing_id) {
          packing_num = item.packing_num
          layup_num = item.layup_num
        }
      })
      this.form.box_number = Math.ceil(
        parseInt(this.form.number) / parseInt(packing_num)
      )
      this.form.story = Math.ceil(
        parseInt(this.form.number) / parseInt(packing_num) / parseInt(layup_num)
      )
    },
    ck_info() {
      console.log(this.form.packing_id)
      this.selectPro.forEach((item) => {
        if (item.goods_name === this.form.goods_name) {
          this.form.goods_id = item.goods_id
        }
      })
      getInventory({
        warehouse_out_id: this.warehouseoutid,
        goods_id: this.form.goods_id,
        packing_id: this.form.packing_id
      }).then((response) => {
        if (response.code === 10000) {
          this.Qty = response.data.Good_Qty
          this.QList = response.data
        }
      })
      if (!this.form.number) {
        return false
      }
      if (this.selectPick.length == 0) {
        return false
      }
      var packing_num = 0
      var data = JSON.parse(JSON.stringify(this.selectPick))
      data.forEach((item) => {
        if (item.value == this.form.packing_id) {
          packing_num = item.packing_num
        }
      })
      this.form.box_number = Math.ceil(
        parseInt(this.form.number) / parseInt(packing_num)
      )
    },
    /** 初始：查询商品 */
    getGoods() {
      const that = this
      this.isLoad = true
      getGoods().then((response) => {
        if (response.code === 10000) {
          that.selectPro = JSON.parse(JSON.stringify(response.data))
          that.selectPro.forEach((item) => {
            item.label = item.goods_name
            item.value = item.goods_name
            that.isLoad = false
          })
        }
      })
    },
    /** 动态获取商品规格 */
    getPacking(type) {
      var that = this
      this.Qty = false
      if (type == 1) {
        delete this.form.packing_id
        console.log(this.form.goods_name, that.selectPro)
        that.selectPro.forEach(item => {
          if (this.form.goods_name == item.goods_name) {
            this.form.goods_id = item.goods_id
          }
        })
      }
      getPacking({ goods_name: this.form.goods_name }).then((response) => {
        if (response.code === 10000) {
          var data = JSON.parse(JSON.stringify(response.data))
          data.forEach((item) => {
            console.log(item)
            item.label = item.label
            // item.value = item.packing_id
            item.value = item.packing_id
            // item.num = item.packing_num
          })
          this.selectPick = data
          console.log(2, this.selectPick)
          this.showSku = true
          this.selectPro.forEach((item) => {
            if (item.goods_name === this.form.goods_name) {
              that.sku = item.sku
            }
          })
        }
      })
    },
    submitForm(formName) {
      const that = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (that.title === '添加') {
            this.add()
          } else {
            this.edit()
          }
          this.editBtnDisabled = true
        } else {
          return false
        }
      })
    },
    getProductpPcking() {
      const that = this
      getGPSelect().then((response) => {
        if (response.code === 10000) {
          this.selectData = JSON.parse(JSON.stringify(response.data))
          that.selectList(this.selectData)
        }
      })
    },
    // 箱子改变
    box_number_change() {
      // eslint-disable-next-line use-isnan
      if (
        Number(this.form.number) !== NaN &&
        Number(this.form.number) > 0 &&
        this.form.packing_id &&
        this.form.packing_id.length === 2
      ) {
        // eslint-disable-next-line no-unused-vars
        var num = 0
        // console.log(JSON.stringify(this.selectData))
        for (var i = 0; i < this.selectData.length; i++) {
          if (
            this.form.packing_id[0].toString() ===
            this.selectData[i].value.toString()
          ) {
            for (var n = 0; n < this.selectData[i].children.length; n++) {
              // console.log(this.form.packing_id[1], this.selectData[i].children[n].value)
              if (
                this.form.packing_id[1].toString() ===
                this.selectData[i].children[n].value.toString()
              ) {
                num = this.selectData[i].children[n].num

                break
              }
            }
            break
          }
        }
        // console.log('eeeeee', Number(this.form.number), num, Number(this.form.number) / num)
        this.form.box_number = Math.ceil(Number(this.form.number) / num)
      } else {
        this.form.box_number = undefined
      }
    },
    add() {
      const that = this
      const select_no = []

      if (this.form.warehousing_select_no != undefined) {
        this.form.warehousing_select_no.forEach((item) => {
          select_no.push(item[0])
        })
      }

      const info = {
        warehouse_out_id: this.warehouseoutid,
        packing_id: [this.form.goods_name, this.form.packing_id,this.form.goods_id], // this.form.packing_id,
        number: this.form.number,
        box_number: this.form.box_number,
        warehousing_select_no: select_no
      }
      createWarehouseOutSon(info).then((response) => {
        if (response.code === 10000) {
          that.$notify.success(response.message)
          that.$parent.getList()
          that.$emit('update:open', false)
        }
      })
    },
    edit() {
      const that = this

      const select_no = []

      if (this.form.warehousing_select_no != undefined) {
        this.form.warehousing_select_no.forEach((item) => {
          select_no.push(item[0])
        })
      }

      const info = {
        id: this.form.id,
        warehouse_out_id: this.warehouseoutid,
        goods_id: this.form.goods_id,
        packing_id: this.form.packing_id,
        number: this.form.number,
        box_number: this.form.box_number,
        warehousing_select_no: select_no,
        PO: this.form.PO,
        DO: this.form.DO,
        CO: this.form.CO,
        SN: this.form.SN,
        LOT1: this.form.LOT1,
        LOT2: this.form.LOT2,
        LOT3: this.form.LOT3
      }
      console.log(info)
      updateWarehouseOutSon(info).then((response) => {
        if (response.code === 10000) {
          that.$notify.success(response.message)
          that.$parent.getList()
          that.$emit('update:open', false)
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    close() {
      this.$emit('update:open', false)
    },
    /* 处理数据 */
    selectList(data) {
      const replaceKey = function(data) {
        data.forEach(function(row, index, arr) {
          if (row.children !== undefined) {
            const temp = { label: row.goods_name, value: row.goods_id }
            if (row.children.length > 0) {
              temp['children'] = []
              row.children.forEach((item) => {
                const info = {
                  label:
                    '包装数量(' +
                    item.packing_num +
                    ')--包装单位(' +
                    item.unit_name +
                    ')',
                  value: item.packing_id,
                  num: item.packing_num
                }
                temp['children'].push(info)
              })
            }
            arr[index] = temp
            if (arr[index].children && arr[index].children.length > 0) {
              replaceKey(arr[index].children)
            }
          }
        })
      }
      replaceKey(data)
      this.isLoad = false
    },
    pickcode()
    {
      this.back_sn = this.form.back_sn
      this.dialogVisible = true
    },
    handleClose()
    {
      this.dialogVisible = false
    },
    addCode()
    {
      let that = this
      that.back_sn = (that.back_sn.replace(/\n/g, ',')).replace(/ /g,'')
      that.back_sn = that.back_sn.endsWith(',')?that.back_sn.slice(0, -1):that.back_sn
      let len = that.back_sn.split(',').length
      if(len != parseInt(that.form.number)){
        this.$message.error('编号数量错误！')
        return false
      }else{
        const select_no = []

        if (this.form.warehousing_select_no != undefined) {
          this.form.warehousing_select_no.forEach((item) => {
            select_no.push(item[0])
          })
        }
        const info = {
          id: that.form.id,
          back_sn: that.back_sn,
          goods_id: that.form.goods_id,
          packing_id: that.form.packing_id,
          warehouse_out_id: that.warehouseoutid,
          warehousing_select_no: select_no
        }
        updateWarehouseOutSon(info).then((response) => {
          if (response.code === 10000) {
            that.$notify.success(response.message)
            that.$parent.getList()
            that.$emit('update:open', false)
            that.dialogVisible = false
          }
        })
      }
    }
  }
}
</script>

<style>

element.style {
  height: 23px;
}

/* .demo-ruleForm2 {
  display: flex;
  flex-wrap: wrap;
}
.demo-ruleForm2 .el-form-item {
  width: calc(50% - 10px) !important;
  margin-left:0!important;
}
.demo-ruleForm2 .el-form-item .el-input,
.demo-ruleForm2 .el-form-item .el-input__inner{
  width:100%!important;
}
.demo-ruleForm2 .el-form-item__content{
  width:calc(100% - 150px)!important;
} */
.el-button + .el-button {
  margin-left: 0 !important;
}
</style>
<style lang="scss" scoped>
.app-container .el-form input {
  //  width: 150px;
}
// /deep/ .el-form input{
//   width: 150px !important;
// }
.demo-table-expand .el-form-item{
width:25%
}
.el-form-item--small.el-form-item {
    //margin-bottom: 0 !important;
}
::v-deep .select-style {
  .el-select .el-input__inner{
  width:100% !important
}
}
</style>
